package com.tsugun.mediaplayer.view.fragment;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.AlbumModel;
import com.tsugun.mediaplayer.model.entity.ArtistModel;
import com.tsugun.mediaplayer.model.entity.MusicModel;
import com.tsugun.mediaplayer.presenter.provider.ListProvider;
import com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper;
import com.tsugun.mediaplayer.view.activity.IndexActivity;
import com.tsugun.mediaplayer.view.adapter.ViewPagerAdapter;
import com.tsugun.mediaplayer.view.fragment.subfragment.AlbumListFragment;
import com.tsugun.mediaplayer.view.fragment.subfragment.SimilarListFragment;
import com.tsugun.mediaplayer.view.fragment.subfragment.SongListFragment;
import com.tsugun.mediaplayer.view.fragment.subfragment.SubBaseFragment;
import com.tsugun.mediaplayer.view.loader.MediaImageLoad;
import com.tsugun.mediaplayer.view.widget.SlidingRelativeLayout;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 歌手界面
 * Created by shize on 2017/4/20.
 */

public class ArtistFragment extends BaseFragment {

    public static final String INTENT_STRING_ARTIST_TITLE = "com.TsuGun.MediaPlayer.intent.artist.title";

    @BindView(R.id.id_sliding_body)
    SlidingRelativeLayout mSlidingLayout;
    @BindView(R.id.id_bg_night)
    RelativeLayout mBgLayout;
    @BindView(R.id.id_artist_toolbar_layout)
    CollapsingToolbarLayout mCollapsing;
    @BindView(R.id.id_artist_image_head)
    ImageView mIvHead;
    @BindView(R.id.id_artist_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_artist_tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.id_artist_view_pager)
    ViewPager mViewPager;
    @BindView(R.id.id_artist_fab)
    FloatingActionButton mFab;

    private View mFragment;
    private IndexActivity indexActivity;
    private Unbinder unbinder;

    private ListProvider mProvider;
    private List<SubBaseFragment> mFragments;
    private List<String> mTitles;
    private String mArtist;
    private List<MusicModel> mSongs;
    private List<AlbumModel> mAlbums;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mFragment != null) {
            return mFragment;
        }
        mFragment = inflater.inflate(R.layout.fragment_artist, container, false);
        indexActivity = (IndexActivity) container.getContext();
        unbinder = ButterKnife.bind(this, mFragment);

        initData();
        bindData();
        return mFragment;
    }

    @Override
    protected void setSettingTheme() {
        int themeColor = indexActivity.getThemeColor();
        mFab.setBackgroundTintList(new ColorStateList(new int[][]{new int[0]}, new int[]{themeColor}));
        mCollapsing.setContentScrimColor(themeColor);
        mBgLayout.setBackgroundColor(SettingPreferenceHelper.INSTANCE.getBACKGROUND_COLOR());
    }

    /**
     * 初始化数据
     */
    private void initData() {
        mProvider = indexActivity.getListProvider();
        mArtist = getArguments().getString(INTENT_STRING_ARTIST_TITLE);
        mTitles = new ArrayList<>();
        mTitles.add(getString(R.string.str_tab_song));
        mTitles.add(getString(R.string.str_tab_album));
        mTitles.add(getString(R.string.str_tab_similar));
        mFragments = new ArrayList<>();
        mFragments.add(new SongListFragment());
        mFragments.add(new AlbumListFragment());
        mFragments.add(new SimilarListFragment());
    }

    /**
     * 绑定数据
     */
    private void bindData() {
        bindSliding();
        initToolbar();
        setHead();
        initPage();
        initFragmentData();
        bindFab();
    }

    private void bindSliding() {
        mSlidingLayout.setOnSlidingListener(new SlidingRelativeLayout.OnSlidingListener() {
            @Override
            public void onDown() {

            }

            @Override
            public void onAnimationEnd(boolean isClose) {
                if (isClose) {
                    indexActivity.getMusicFragmentManager().backFragment();
                }
            }
        });
    }

    /**
     * 绑定悬浮按钮
     */
    private void bindFab() {
        setFabIcon(mProvider.isFollow(mArtist));
        mFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFabIcon(mProvider.setFollow(mArtist));
            }
        });
    }

    /**
     * 绑定歌手头像
     */
    private void setHead() {
        Collection<ArtistModel> artistModels = (Collection<ArtistModel>) mProvider.searchArtistByName(mArtist);
        if (artistModels.size() > 0) {
            List<ArtistModel> list = (List<ArtistModel>) artistModels;
            MediaImageLoad.netArtistImageLoad(list.get(0), mIvHead);
        }
    }

    /**
     * 初始化toolbar
     */
    private void initToolbar() {
        mToolbar.setTitle(mArtist);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                indexActivity.getMusicFragmentManager().backFragment();
            }
        });
    }

    /**
     * 初始化pager
     */
    private void initPage() {
        for (String title : mTitles) {
            mTabLayout.addTab(mTabLayout.newTab().setText(title));
        }
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(
                indexActivity.getSupportFragmentManager(), mFragments, mTitles);
        mViewPager.setAdapter(viewPagerAdapter);
        mViewPager.setOffscreenPageLimit(2);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    /**
     * 初始化fragment数据
     */
    private void initFragmentData() {
        if (mProvider == null) {
            return;
        }
        mSongs = (List<MusicModel>) mProvider.getMusicByArtist(mArtist);
        mAlbums = (List<AlbumModel>) mProvider.getAlbumByArtist(mArtist);
        ((SongListFragment) mFragments.get(0)).setResult(mSongs);
        ((AlbumListFragment) mFragments.get(1)).setResult(mAlbums);
        ((SimilarListFragment) mFragments.get(2)).setListProvider(mProvider);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        clearFragments(indexActivity, mFragments);
        super.onDestroy();
        unbinder.unbind();
    }

    /**
     * 设置悬浮按钮图标
     *
     * @param isFollow 是否关注
     */
    public void setFabIcon(boolean isFollow) {
        if (!isFollow) {
            mFab.setImageResource(R.drawable.ic_add_light);
        } else {
            mFab.setImageResource(R.drawable.ic_checked);
        }
    }
}
